<!DOCTYPE html>
<html lang="zh-CN" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>老油条 | 老油条</title>
    <meta name="description" content="个人的成长之路，包含前端常用知识、源码阅读笔记、各种奇淫技巧、日常提效工具等">
    <link rel="preload stylesheet" href="/assets/style.78397f57.css" as="style">
    <link rel="modulepreload" href="/assets/app.d59c877d.js">
    <link rel="modulepreload" href="/assets/document_java_Java 集合.md.adfefbd7.lean.js">
    
    <meta name="theme-color" content="#3eaf7c">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <link rel="apple-touch-icon" href="/favicon.ico">
  <link rel="mask-icon" href="/favicon.ico" color="#3eaf7c">
  <meta name="msapplication-TileImage" content="/favicon.ico">
  <meta name="msapplication-TileColor" content="#000000">
  <script id="check-dark-light">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-37f477df><!--[--><!--]--><!--[--><span tabindex="-1" data-v-4fc2cfce></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-4fc2cfce> Skip to content </a><!--]--><!----><header class="VPNav" data-v-37f477df data-v-8263d7e6><div class="VPNavBar has-sidebar" data-v-8263d7e6 data-v-25a29d8b><div class="container" data-v-25a29d8b><div class="title" data-v-25a29d8b><div class="VPNavBarTitle has-sidebar" data-v-25a29d8b data-v-bbc64f2f><a class="title" href="/" data-v-bbc64f2f><!--[--><!--]--><!--[--><img class="VPImage logo" src="/logo.png" alt data-v-7770c775><!--]--><!--[-->老油条<!--]--><!--[--><!--]--></a></div></div><div class="content" data-v-25a29d8b><div class="curtain" data-v-25a29d8b></div><div class="content-body" data-v-25a29d8b><!--[--><!--]--><!----><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-25a29d8b data-v-349f684d><span id="main-nav-aria-label" class="visually-hidden" data-v-349f684d>Main Navigation</span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/" data-v-349f684d data-v-3daa90a7 data-v-f98ca989><!--[-->首页<!--]--><!----></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/document/" data-v-349f684d data-v-3daa90a7 data-v-f98ca989><!--[-->个人文档<!--]--><!----></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/nav/" data-v-349f684d data-v-3daa90a7 data-v-f98ca989><!--[-->网站导航<!--]--><!----></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/video/" data-v-349f684d data-v-3daa90a7 data-v-f98ca989><!--[-->剪影教程<!--]--><!----></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-25a29d8b data-v-8c0bdc46><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" aria-label="toggle dark mode" aria-checked="false" data-v-8c0bdc46 data-v-d714f3df data-v-93e21d5e><span class="check" data-v-93e21d5e><span class="icon" data-v-93e21d5e><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-d714f3df><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-d714f3df><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div><!----><div class="VPFlyout VPNavBarExtra extra" data-v-25a29d8b data-v-6fc9b924 data-v-b1f35f78><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-b1f35f78><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="icon" data-v-b1f35f78><circle cx="12" cy="12" r="2"></circle><circle cx="19" cy="12" r="2"></circle><circle cx="5" cy="12" r="2"></circle></svg></button><div class="menu" data-v-b1f35f78><div class="VPMenu" data-v-b1f35f78 data-v-21275a90><!----><!--[--><!--[--><!----><div class="group" data-v-6fc9b924><div class="item appearance" data-v-6fc9b924><p class="label" data-v-6fc9b924>外观</p><div class="appearance-action" data-v-6fc9b924><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" aria-label="toggle dark mode" aria-checked="false" data-v-6fc9b924 data-v-d714f3df data-v-93e21d5e><span class="check" data-v-93e21d5e><span class="icon" data-v-93e21d5e><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-d714f3df><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-d714f3df><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div></div></div><!----><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-25a29d8b data-v-89c94d3e><span class="container" data-v-89c94d3e><span class="top" data-v-89c94d3e></span><span class="middle" data-v-89c94d3e></span><span class="bottom" data-v-89c94d3e></span></span></button></div></div></div></div><!----></header><div class="VPLocalNav" data-v-37f477df data-v-c87b4d81><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-c87b4d81><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="menu-icon" data-v-c87b4d81><path d="M17,11H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,11,17,11z"></path><path d="M21,7H3C2.4,7,2,6.6,2,6s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,7,21,7z"></path><path d="M21,15H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,15,21,15z"></path><path d="M17,19H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,19,17,19z"></path></svg><span class="menu-text" data-v-c87b4d81>Menu</span></button><a class="top-link" href="#" data-v-c87b4d81>返回顶部</a></div><aside class="VPSidebar" data-v-37f477df data-v-47b3bdca><div class="curtain" data-v-47b3bdca></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-47b3bdca><span class="visually-hidden" id="sidebar-aria-label" data-v-47b3bdca> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="group" data-v-47b3bdca><section class="VPSidebarItem level-0" data-v-47b3bdca data-v-1d351fe6><div class="item" role="button" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link" data-v-1d351fe6 data-v-f98ca989><!--[--><h2 class="text" data-v-1d351fe6>个人文档</h2><!--]--><!----></a><!----></div><div class="items" data-v-1d351fe6><!--[--><div class="VPSidebarItem level-1 is-link" data-v-1d351fe6 data-v-1d351fe6><div class="item" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link link" href="/document/" data-v-1d351fe6 data-v-f98ca989><!--[--><p class="text" data-v-1d351fe6>简介</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-1d351fe6 data-v-1d351fe6><div class="item" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link link" href="/video/dir" data-v-1d351fe6 data-v-f98ca989><!--[--><p class="text" data-v-1d351fe6>目录</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-47b3bdca><section class="VPSidebarItem level-0 has-active" data-v-47b3bdca data-v-1d351fe6><div class="item" role="button" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link" data-v-1d351fe6 data-v-f98ca989><!--[--><h2 class="text" data-v-1d351fe6>java</h2><!--]--><!----></a><!----></div><div class="items" data-v-1d351fe6><!--[--><div class="VPSidebarItem level-1 is-link is-active has-active" data-v-1d351fe6 data-v-1d351fe6><div class="item" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link link" href="/document/java/Java%20%E9%9B%86%E5%90%88" data-v-1d351fe6 data-v-f98ca989><!--[--><p class="text" data-v-1d351fe6>Java集合 </p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-1d351fe6 data-v-1d351fe6><div class="item" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link link" href="/document/java/Stream%E8%AF%A6%E8%A7%A3" data-v-1d351fe6 data-v-f98ca989><!--[--><p class="text" data-v-1d351fe6>Stream详解</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-47b3bdca><section class="VPSidebarItem level-0" data-v-47b3bdca data-v-1d351fe6><div class="item" role="button" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link" data-v-1d351fe6 data-v-f98ca989><!--[--><h2 class="text" data-v-1d351fe6>SpringBoot</h2><!--]--><!----></a><!----></div><div class="items" data-v-1d351fe6><!--[--><div class="VPSidebarItem level-1 is-link" data-v-1d351fe6 data-v-1d351fe6><div class="item" data-v-1d351fe6><div class="indicator" data-v-1d351fe6></div><a class="VPLink link link" href="/document/java/SpringBoot%20@Async%E7%AE%80%E4%BB%8B" data-v-1d351fe6 data-v-f98ca989><!--[--><p class="text" data-v-1d351fe6>注解@Async简介</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-37f477df data-v-c336b2e4><div class="VPDoc has-sidebar has-aside" data-v-c336b2e4 data-v-7f422fda><div class="container" data-v-7f422fda><div class="aside" data-v-7f422fda><div class="aside-curtain" data-v-7f422fda></div><div class="aside-container" data-v-7f422fda><div class="aside-content" data-v-7f422fda><div class="VPDocAside" data-v-7f422fda data-v-53f738a8><!--[--><!--]--><!--[--><!--]--><div class="VPDocAsideOutline" data-v-53f738a8 data-v-5687e8fd><div class="content" data-v-5687e8fd><div class="outline-marker" data-v-5687e8fd></div><div class="outline-title" data-v-5687e8fd>本页目录</div><nav aria-labelledby="doc-outline-aria-label" data-v-5687e8fd><span class="visually-hidden" id="doc-outline-aria-label" data-v-5687e8fd> Table of Contents for current page </span><ul class="root" data-v-5687e8fd data-v-2416c4df><!--[--><!--]--></ul></nav></div></div><!--[--><!--]--><div class="spacer" data-v-53f738a8></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-7f422fda><div class="content-container" data-v-7f422fda><!--[--><!--]--><main class="main" data-v-7f422fda><div style="position:relative;" class="vp-doc _document_java_Java%20%E9%9B%86%E5%90%88" data-v-7f422fda><div><h2 id="一、map集合" tabindex="-1">一、Map集合 <a class="header-anchor" href="#一、map集合" aria-hidden="true">#</a></h2><ol><li>Map 是一种键-值对（key-value）集合，Map 集合中的每一个元素都包含一个键（key）对象和一个值（value）对象。用于保存具有映射关系的数据。</li><li>Map 集合里保存着两组值，一组值用于保存 Map 里的 key，另外一组值用于保存 Map 里的 value，key 和 value 都可以是任何引用类型的数据。Map 的 key 不允许重复，value 可以重复，即同一个 Map 对象的任何两个 key 通过 equals 方法比较总是返回 false。</li><li>Map 中的 key 和 value 之间存在单向一对一关系，即通过指定的 key，总能找到唯一的、确定的 value。从 Map 中取出数据时，只要给出指定的 key，就可以取出对应的 value。</li><li><strong>Map 接口主要有两个实现类：<code>HashMap</code> 类和 <code>TreeMap</code> 类。其中，<code>HashMap</code> 类按哈希算法来存取键对象，而 <code>TreeMap</code> 类可以对键对象进行排序。</strong></li></ol><h3 id="_1-1-遍历map集合" tabindex="-1">1.1 遍历Map集合 <a class="header-anchor" href="#_1-1-遍历map集合" aria-hidden="true">#</a></h3><blockquote><p>在 for 循环中使用 entries 实现 Map 的遍历</p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#F97583;">public</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">static</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">void</span><span style="color:#E1E4E8;"> </span><span style="color:#B392F0;">main</span><span style="color:#E1E4E8;">(</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">[] args) {</span></span>
<span class="line"><span style="color:#E1E4E8;">    Map&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">, </span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; map </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> HashMap&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">, </span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt;();</span></span>
<span class="line"><span style="color:#E1E4E8;">    map.</span><span style="color:#B392F0;">put</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;aa&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;张三&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">    map.</span><span style="color:#B392F0;">put</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;bb&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;李四&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">    </span><span style="color:#F97583;">for</span><span style="color:#E1E4E8;"> (Map.Entry&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">, </span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; entry </span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;"> map.</span><span style="color:#B392F0;">entrySet</span><span style="color:#E1E4E8;">()) {</span></span>
<span class="line"><span style="color:#E1E4E8;">        String mapKey </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> entry.</span><span style="color:#B392F0;">getKey</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#E1E4E8;">        String mapValue </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> entry.</span><span style="color:#B392F0;">getValue</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#E1E4E8;">        System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(mapKey </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> </span><span style="color:#9ECBFF;">&quot;：&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> mapValue);</span></span>
<span class="line"><span style="color:#E1E4E8;">    }</span></span>
<span class="line"><span style="color:#E1E4E8;">}</span></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><blockquote><p>使用 for-each 循环遍历 key 或者 values，一般适用于只需要 Map 中的 key 或者 value 时使用。性能上比 entrySet 较好。</p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#E1E4E8;">Map&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">, </span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; map </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> HashMap&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">, </span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt;();</span></span>
<span class="line"><span style="color:#E1E4E8;">map.</span><span style="color:#B392F0;">put</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;aa&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;张三&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">map.</span><span style="color:#B392F0;">put</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;bb&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;李四&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#6A737D;">// 打印键集合</span></span>
<span class="line"><span style="color:#F97583;">for</span><span style="color:#E1E4E8;"> (String key </span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;"> map.</span><span style="color:#B392F0;">keySet</span><span style="color:#E1E4E8;">()) {</span></span>
<span class="line"><span style="color:#E1E4E8;">    System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(key);</span></span>
<span class="line"><span style="color:#E1E4E8;">}</span></span>
<span class="line"><span style="color:#6A737D;">// 打印值集合</span></span>
<span class="line"><span style="color:#F97583;">for</span><span style="color:#E1E4E8;"> (String value </span><span style="color:#F97583;">:</span><span style="color:#E1E4E8;"> map.</span><span style="color:#B392F0;">values</span><span style="color:#E1E4E8;">()) {</span></span>
<span class="line"><span style="color:#E1E4E8;">    System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(value);</span></span>
<span class="line"><span style="color:#E1E4E8;">}</span></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><blockquote><p>使用迭代器（Iterator）遍历</p></blockquote><div class="language-java line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki github-dark" tabindex="0"><code><span class="line"><span style="color:#E1E4E8;">Map&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">, </span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; map </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">new</span><span style="color:#E1E4E8;"> HashMap&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">, </span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt;();</span></span>
<span class="line"><span style="color:#E1E4E8;">map.</span><span style="color:#B392F0;">put</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;aa&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;张三&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">map.</span><span style="color:#B392F0;">put</span><span style="color:#E1E4E8;">(</span><span style="color:#9ECBFF;">&quot;bb&quot;</span><span style="color:#E1E4E8;">, </span><span style="color:#9ECBFF;">&quot;李四&quot;</span><span style="color:#E1E4E8;">);</span></span>
<span class="line"><span style="color:#E1E4E8;">Iterator&lt;Entry&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">, </span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt;&gt; entries </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> map.</span><span style="color:#B392F0;">entrySet</span><span style="color:#E1E4E8;">().</span><span style="color:#B392F0;">iterator</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#F97583;">while</span><span style="color:#E1E4E8;"> (entries.</span><span style="color:#B392F0;">hasNext</span><span style="color:#E1E4E8;">()) {</span></span>
<span class="line"><span style="color:#E1E4E8;">    Entry&lt;</span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">, </span><span style="color:#F97583;">String</span><span style="color:#E1E4E8;">&gt; entry </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> entries.</span><span style="color:#B392F0;">next</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#E1E4E8;">    String key </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> entry.</span><span style="color:#B392F0;">getKey</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#E1E4E8;">    String value </span><span style="color:#F97583;">=</span><span style="color:#E1E4E8;"> entry.</span><span style="color:#B392F0;">getValue</span><span style="color:#E1E4E8;">();</span></span>
<span class="line"><span style="color:#E1E4E8;">    System.out.</span><span style="color:#B392F0;">println</span><span style="color:#E1E4E8;">(key </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> </span><span style="color:#9ECBFF;">&quot;:&quot;</span><span style="color:#E1E4E8;"> </span><span style="color:#F97583;">+</span><span style="color:#E1E4E8;"> value);</span></span>
<span class="line"><span style="color:#E1E4E8;">}</span></span>
<span class="line"></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></div></div></main><!--[--><!--]--><footer class="VPDocFooter" data-v-7f422fda data-v-910ca74c><!----><div class="prev-next" data-v-910ca74c><div class="pager" data-v-910ca74c><a class="pager-link prev" href="/video/dir" data-v-910ca74c><span class="desc" data-v-910ca74c>上一篇</span><span class="title" data-v-910ca74c>目录</span></a></div><div class="has-prev pager" data-v-910ca74c><a class="pager-link next" href="/document/java/Stream%E8%AF%A6%E8%A7%A3" data-v-910ca74c><span class="desc" data-v-910ca74c>下一篇</span><span class="title" data-v-910ca74c>Stream详解</span></a></div></div></footer><!--[--><!--]--></div></div></div></div></div><footer class="VPFooter has-sidebar" data-v-37f477df data-v-57f271d6><div class="container" data-v-57f271d6><p class="message" data-v-57f271d6>我的征途是星辰大海,从此只顾风雨兼程,披荆斩棘。</p><p class="copyright" data-v-57f271d6>Copyright © 2019-present maomao</p></div></footer><!--[--><!--]--></div></div>
    <script>__VP_HASH_MAP__ = JSON.parse("{\"video_001_03_69.md\":\"24ee4e2a\",\"video_001_04_84.md\":\"c9ff651c\",\"video_001_04_88.md\":\"f82e3ba8\",\"video_001_03_72.md\":\"bda20a6f\",\"video_001_03_45.md\":\"4a6da3c9\",\"video_dir.md\":\"594f8a10\",\"video_001_02_25.md\":\"60b00b20\",\"video_001_02_24.md\":\"004eea6a\",\"video_001_03_49.md\":\"349159b7\",\"video_001_02_13.md\":\"4c81051c\",\"video_001_02_32.md\":\"2d2832f4\",\"video_001_02_22.md\":\"ce008882\",\"video_001_02_38.md\":\"429972e9\",\"video_001_03_79.md\":\"3d2d6110\",\"video_002_01.md\":\"23906846\",\"video_001_05_103.md\":\"6a000306\",\"video_001_02_33.md\":\"c759de41\",\"video_001_03_67.md\":\"d906792b\",\"video_001_03_53.md\":\"00e67cb9\",\"video_001_03_80.md\":\"531d3283\",\"video_001_03_57.md\":\"5839b3e3\",\"video_001_04_83.md\":\"9bd1135a\",\"video_001_03_56.md\":\"720f9f39\",\"video_001_03_52.md\":\"8bc042f6\",\"video_001_03_60.md\":\"66640c67\",\"video_001_04_97.md\":\"4f6c4b38\",\"video_001_03_62.md\":\"c45f2772\",\"video_001_02_35.md\":\"40310176\",\"video_001_04_86.md\":\"d386e4d2\",\"video_index.md\":\"53e2e028\",\"video_001_02_16.md\":\"903bc9ee\",\"video_001_04_98.md\":\"2da8a7a1\",\"video_001_01_7.md\":\"fb12b5f4\",\"video_001_02_30.md\":\"7f036e13\",\"video_001_04_99.md\":\"e6e69a97\",\"video_001_03_46.md\":\"7bef5617\",\"video_001_02_14.md\":\"10bfd666\",\"video_001_04_87.md\":\"a9af7db0\",\"video_001_02_26.md\":\"1a1531f9\",\"video_001_03_70.md\":\"63a9da24\",\"video_001_03_78.md\":\"5ae269e7\",\"video_001_02_17.md\":\"a444bd54\",\"video_001_03_51.md\":\"58bd3a1c\",\"video_001_03_43.md\":\"9d415e0b\",\"nav_index.md\":\"ba04b6ec\",\"index.md\":\"cfbd0506\",\"video_001_03_66.md\":\"c4048c68\",\"video_001_05_106.md\":\"5eadbf46\",\"video_001_03_41.md\":\"c39de5e4\",\"video_001_03_63.md\":\"424c930f\",\"video_001_02_12.md\":\"750e295c\",\"video_001_04_92.md\":\"ea0907d3\",\"video_001_04_100.md\":\"d2c2b603\",\"video_001_04_95.md\":\"0d67e61a\",\"video_001_03_77.md\":\"5cb8dc5b\",\"video_001_03_58.md\":\"6df8ce20\",\"video_001_03_44.md\":\"b1a82273\",\"video_001_05_111.md\":\"b875449f\",\"video_001_02_10.md\":\"c14f118a\",\"video_004_01.md\":\"cbd007c4\",\"document_index.md\":\"d81f3f7a\",\"video_001_03_65.md\":\"a2026925\",\"video_001_04_94.md\":\"adc3de3d\",\"video_001_01_6.md\":\"8c8b664d\",\"video_001_02_27.md\":\"5a794fcc\",\"video_001_03_74.md\":\"6a8abe02\",\"video_001_03_75.md\":\"9e72b009\",\"video_001_03_76.md\":\"b56a1116\",\"video_001_01_9.md\":\"e9638db1\",\"video_001_02_34.md\":\"c07bb664\",\"video_001_03_71.md\":\"23b34f7f\",\"video_001_02_23.md\":\"a64bac66\",\"video_001_03_40.md\":\"66a61f43\",\"video_001_01_5.md\":\"8dc2ee95\",\"video_001_02_28.md\":\"88dd7851\",\"video_001_02_31.md\":\"79e11f7c\",\"video_001_03_48.md\":\"1409b645\",\"video_001_05_105.md\":\"f285a2e7\",\"video_001_04_90.md\":\"91c1790c\",\"video_001_02_39.md\":\"7d04a723\",\"video_001_03_61.md\":\"d15404a7\",\"video_001_05_104.md\":\"bdc64109\",\"video_001_02_37.md\":\"ed548bc3\",\"video_001_02_29.md\":\"8705a744\",\"video_001_04_91.md\":\"c2e80b6c\",\"video_001_03_73.md\":\"59e59a68\",\"video_001_04_89.md\":\"bff89466\",\"video_003_01.md\":\"93b16f80\",\"video_001_02_11.md\":\"1b632398\",\"video_001_03_59.md\":\"f9fb15f1\",\"video_001_05_110.md\":\"219dd143\",\"video_001_02_36.md\":\"db5b0aea\",\"video_001_02_19.md\":\"c2182602\",\"video_001_03_68.md\":\"f92433e5\",\"video_001_04_82.md\":\"cb5d073f\",\"video_001_02_21.md\":\"680b89a5\",\"document_java_java 集合.md\":\"adfefbd7\",\"video_001_04_93.md\":\"cad2027c\",\"video_001_03_64.md\":\"7b016b05\",\"video_001_03_54.md\":\"1d5d7764\",\"video_001_05_112.md\":\"4632735a\",\"video_005_01.md\":\"6fa10e68\",\"video_001_04_96.md\":\"c87ad1e3\",\"video_001_02_15.md\":\"d3f7d717\",\"video_001_01_8.md\":\"c96a0a42\",\"video_001_05_109.md\":\"332b1d16\",\"video_001_01_4.md\":\"0a7f8b8c\",\"video_001_03_50.md\":\"18a01d2f\",\"video_001_02_18.md\":\"0a76be5f\",\"video_001_02_20.md\":\"f3b28a78\",\"video_001_03_47.md\":\"6bedeaf8\",\"document_java_springboot @async简介.md\":\"715420a9\",\"video_001_04_85.md\":\"1bea96c8\",\"video_001_05_107.md\":\"dee3f204\",\"video_001_03_55.md\":\"58004b02\",\"video_001_03_81.md\":\"7d8c84b6\",\"video_001_05_108.md\":\"92d9e16a\",\"video_001_03_42.md\":\"d3a61904\",\"video_001_05_102.md\":\"d76504b4\",\"video_001_05_101.md\":\"86e3e75d\",\"video_001_01_3.md\":\"a879eb45\",\"document_java_stream详解.md\":\"04298379\"}")</script>
    <script type="module" async src="/assets/app.d59c877d.js"></script>
    
  </body>
</html>